package cn.stylefeng.guns.modular.basic.service.impl ;

import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.ArchiveBox;
import cn.stylefeng.guns.modular.basic.entity.ArchiveCabinet;
import cn.stylefeng.guns.modular.basic.entity.ArchiveLayer;
import cn.stylefeng.guns.modular.basic.mapper.ArchiveCabinetMapper;
import cn.stylefeng.guns.modular.basic.model.params.ArchiveCabinetParam;
import cn.stylefeng.guns.modular.basic.model.params.ArchiveLayerParam;
import cn.stylefeng.guns.modular.basic.model.result.ArchiveCabinetResult;
import cn.stylefeng.guns.modular.basic.service.ArchiveBoxService;
import cn.stylefeng.guns.modular.basic.service.ArchiveCabinetService;
import cn.stylefeng.guns.modular.basic.service.ArchiveLayerService;
import cn.stylefeng.guns.modular.manager.entity.ArchiveInfo;
import cn.stylefeng.guns.modular.manager.service.ArchiveInfoService;
import cn.stylefeng.guns.utils.ServiceAssert;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 档案柜信息表 服务实现类
 * </p>
 *
 * @author blesslp
 * @since 2019-11-17
 */
@Service
public class ArchiveCabinetServiceImpl extends ServiceImpl<ArchiveCabinetMapper, ArchiveCabinet> implements ArchiveCabinetService {

    @Autowired
    private ArchiveInfoService archiveInfoService;
    @Autowired
    private ArchiveBoxService archiveBoxService;
    @Autowired
    private ArchiveLayerService archiveLayerService;
    @Override
    public boolean hasArchiveInfo(String key) {
        final List<String> boxids = this.archiveBoxService.lambdaQuery()
                .eq(ArchiveBox::getSsdagid, key)
                .select(ArchiveBox::getId)
                .list().parallelStream().map(ArchiveBox::getId).collect(Collectors.toList());

        return this.archiveInfoService.lambdaQuery()
                .in(ArchiveInfo::getDahid,boxids)
                .count() > 0;
    }
    @Override
    public void add(ArchiveCabinetParam param){
        ArchiveCabinet entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional
    public void delete(ArchiveCabinetParam param){
        final Serializable key = getKey(param);
        boolean isSuccess = this.removeById(key);
        String TIPS = "删除失败";
        ServiceAssert.assetTrue(isSuccess,TIPS);

        final List<ArchiveLayer> list = this.archiveLayerService.lambdaQuery()
                .eq(ArchiveLayer::getSsdagid, key)
                .list();

        for (ArchiveLayer archiveLayer : list) {
            ArchiveLayerParam archiveLayerParam = new ArchiveLayerParam();
            archiveLayerParam.setId(archiveLayer.getId());
            this.archiveLayerService.delete(archiveLayerParam);
        }
    }

    @Override
    public void update(ArchiveCabinetParam param){
        ArchiveCabinet oldEntity = getOldEntity(param);
        ArchiveCabinet newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ArchiveCabinetResult findBySpec(ArchiveCabinetParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<ArchiveCabinetResult> findListBySpec(ArchiveCabinetParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ArchiveCabinetParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ArchiveCabinetParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ArchiveCabinet getOldEntity(ArchiveCabinetParam param) {
        return this.getById(getKey(param));
    }

    private ArchiveCabinet getEntity(ArchiveCabinetParam param) {
        ArchiveCabinet entity = new ArchiveCabinet();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<ZTreeNode> tree(ArchiveCabinetParam param) {
        return this.baseMapper.tree(param);
    }

    @Override
    public String getMaxCode(String roomId) {
        return this.baseMapper.getMaxCode(roomId);
    }
}
